package com.ruoyi.web.controller.jjm;

import java.util.List;

import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.jjm.service.IClientUserService;
import com.ruoyi.system.domain.SysUser;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.jjm.domain.Baolianren;
import com.ruoyi.jjm.service.IBaolianrenService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 包联人Controller
 * 
 * @author tsr
 * @date 2020-07-15
 */
@Controller
@RequestMapping("/jjm/baolianren")
public class BaolianrenController extends BaseController
{
    private String prefix = "jjm/baolianren";

    @Autowired
    private IBaolianrenService baolianrenService;
    @Autowired
    private IClientUserService clientUserService;

//    @RequiresPermissions("jjm:baolianren:view")
    @GetMapping()
    public String baolianren()
    {
        return prefix + "/baolianren";
    }

    /**
     * 查询包联人列表
     */
//    @RequiresPermissions("jjm:baolianren:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Baolianren baolianren)
    {
        baolianren.setAreaType(4L);
        SysUser user = ShiroUtils.getSysUser();
        baolianren.setAreaId(user.getDeptId());
        startPage();
        List<Baolianren> list = baolianrenService.selectBaolianrenList(baolianren);
        if(CollectionUtils.isNotEmpty(list)){
            for(Baolianren baolianren1 : list){
                Long countNum = clientUserService.countBaolianHouseNum(baolianren1.getId());
                baolianren1.setHouseholds(countNum);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出包联人列表
     */
    @RequiresPermissions("jjm:baolianren:export")
    @Log(title = "包联人", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Baolianren baolianren)
    {
        List<Baolianren> list = baolianrenService.selectBaolianrenList(baolianren);
        ExcelUtil<Baolianren> util = new ExcelUtil<Baolianren>(Baolianren.class);
        return util.exportExcel(list, "baolianren");
    }

    /**
     * 新增包联人
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存包联人
     */
    @RequiresPermissions("jjm:baolianren:add")
    @Log(title = "包联人", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Baolianren baolianren)
    {
        return toAjax(baolianrenService.insertBaolianren(baolianren));
    }

    /**
     * 修改包联人
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Baolianren baolianren = baolianrenService.selectBaolianrenById(id);
        mmap.put("baolianren", baolianren);
        return prefix + "/edit";
    }

    /**
     * 修改保存包联人
     */
    @RequiresPermissions("jjm:baolianren:edit")
    @Log(title = "包联人", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Baolianren baolianren)
    {
        return toAjax(baolianrenService.updateBaolianren(baolianren));
    }

    /**
     * 删除包联人
     */
    @RequiresPermissions("jjm:baolianren:remove")
    @Log(title = "包联人", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(baolianrenService.deleteBaolianrenByIds(ids));
    }
}
